﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class CameraControl : MonoBehaviour {

    public static CameraControl Instance;

    private GameObject mainCamera;
    private AudioSource audios;

    public bool firstTouch = false;
    public AudioClip OpenDoorClip;
    public AudioClip RunClip;

	// Use this for initialization
	void Start ()
    {
        Instance = this;
        
        mainCamera = GameObject.Find("Main Camera");
        audios = GameObject.Find("Main Camera").GetComponent<AudioSource>();
	}
	

	// Update is called once per frame
	void OnGUI ()
    {
        if (Event.current.type == EventType.mouseDown && firstTouch==true)
        {
            transform.DOMove(new Vector3(0,2,11),2);

            audios.clip = RunClip;
            audios.Play();
            audios.loop = true;

            PlayerState.Instance.SetState(PlayerState.State.RUN,PlayerControl.Instance.target2,2);

            StartCoroutine("CloseMusic");

            firstTouch = false;
        }

    }

    IEnumerator CloseMusic()
    {
        yield return new WaitForSeconds(2);
        audios.Stop();
    }

    public void LeaveDoor()
    {
        //相机移动
        transform.DOMove(new Vector3(0,2,26),2);
        //开门
        StartCoroutine("OpenDoor");
    }
    
    IEnumerator OpenDoor()
    {
        yield return new WaitForSeconds(2);
        //开门特效
        GameObject effect= Instantiate(Resources.Load("Prefabs/Effect/Level003_GateLight"),GameObject.Find("OutDoor/OutDoor").transform) as GameObject;
        effect.transform.localPosition = new Vector3(0,0,0);

        GameObject door1 = GameObject.Find("Door01");
        GameObject door2 = GameObject.Find("Door02");
        door1.transform.DOLocalMoveX(-4, 3);
        door2.transform.DOLocalMoveX(2.5f, 3);
        //transform.DOShakePosition(0.8f,4);
        transform.DOShakeRotation(5,1);

        audios.clip = OpenDoorClip;
        audios.Play();
        audios.loop = false;

        //分数
        GameObject.Find("UI_Game").SendMessage("AddNum", 3);
        //关卡解锁
        if (!LevelSingleton.GetInstance().levelData.ContainsKey(3))
        {
            LevelSingleton.GetInstance().levelData.Add(3, 1);
        }
        
        PlayerPrefs.SetInt("3", 1);


        //对话
        yield return new WaitForSeconds(2);
        PlayerControl.Instance.Dialog();

        
    }
}
